#!/bin/sh
 
OTA_EXE=/root/gateway.update
MAIN_EXE=/usr/bin/gateway

start() {
    #替换程序包
    #判断是否存在一个下载了的更新程序  /root/gateway.update
    #如果有 1 把旧的程序删除 或者重命名 
    #      2  把更新的程序 mv 到 /usr/bin/gateway  
    #      3 给执行u权限
    if [ -f "/root/gateway.update" ]
    then 
        rm -f  $MAIN_EXE
        mv  $OTA_EXE  $MAIN_EXE
        chmod +x $MAIN_EXE
    fi

    #启动程序
	printf "Starting gateway sshd: "
 
	start-stop-daemon -S -q -p /var/run/gateway.pid \
		--exec /usr/bin/gateway -- daemon
	[ $? = 0 ] && echo "OK" || echo "FAIL"
}
stop() {
	printf "Stopping gateway sshd: "
	start-stop-daemon -K -q -p /var/run/gateway.pid
	[ $? = 0 ] && echo "OK" || echo "FAIL"
}
restart() {
	stop
	start
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart|reload)
  	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
